package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.study.entity.EduCourse;
import com.xmy.study.entity.EduShutnum;
import com.xmy.study.entity.EduVideo;
import com.xmy.study.service.IEduCourseService;
import com.xmy.study.service.IEduVideoService;
import com.xmy.study.service.IShutnumService;
import com.xmy.study.util.Code;
import com.xmy.study.util.PageResult;
import com.xmy.study.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 关数 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-09-08
 */
@RestController
@RequestMapping("/api/web/shutnum")
public class ShutnumController {
    @Autowired
    IEduCourseService iEduCourseService;
    @Autowired
    IShutnumService iEduShutnumService;

    @Autowired
    IEduVideoService eduVideoService;
    /**
     * 获取关数
     * @return
     */
    @GetMapping("/getShutNumList")
    @Transactional
    public Result getShutNumList(@RequestParam(defaultValue = "0",name = "courseId") Long courseId, @RequestParam(defaultValue = "0",name = "pageNum") Long pageNum, @RequestParam(defaultValue = "25",name = "pageSize") Long pageSize){
        EduCourse eduCourse = iEduCourseService.getById(courseId);
        if (eduCourse == null) {
            return new Result(Code.OK,null,"暂无数据！");
        }
        /**
         * 页码为0时默认应该在第几页
         */
        if (pageNum == 0 ){
            QueryWrapper<EduShutnum> eduShutnumQueryWrapper = new QueryWrapper<>();
            eduShutnumQueryWrapper.eq("course_id",eduCourse.getId());
            eduShutnumQueryWrapper.orderByAsc("num");
            Long count = iEduShutnumService.count(eduShutnumQueryWrapper);
            pageNum = count/pageSize + 1;
        }

        /**
         * 获取关数
         */
        IPage<EduShutnum> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<EduShutnum> eduShutnumQueryWrapper = new QueryWrapper<>();
        eduShutnumQueryWrapper.eq("course_id",eduCourse.getId());
        eduShutnumQueryWrapper.eq("status",1);
        eduShutnumQueryWrapper.orderByAsc("num");
        iEduShutnumService.page(iPage,eduShutnumQueryWrapper);
        ArrayList<Object> shutnumArr = new ArrayList<>();
        for (EduShutnum eduShutnum: iPage.getRecords()) {
            HashMap<Object, Object> shutnumMap = new HashMap<>();
            shutnumMap.put("id", eduShutnum.getId());
            shutnumMap.put("name", eduShutnum.getName());
            shutnumMap.put("courseId", eduShutnum.getCourseId());
            shutnumMap.put("num", eduShutnum.getNum());
            shutnumMap.put("status", eduShutnum.getStatus());
            shutnumMap.put("sort", eduShutnum.getSort());
            shutnumMap.put("createdAt", eduShutnum.getCreatedAt());
            shutnumMap.put("level", 1);
            ArrayList<Object> videoArr = new ArrayList<>();
            QueryWrapper<EduVideo> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.inSql("id", "SELECT vedio_id FROM edu_shutnum_video WHERE course_id = " + eduCourse.getId() + " AND shutnum_id = " + eduShutnum.getId() + " AND deleted = 0");
            objectQueryWrapper.orderByAsc("sort");
            List<EduVideo> list = eduVideoService.list(objectQueryWrapper);
            for (EduVideo eduVideo: list) {
                HashMap<Object, Object> videoMap = new HashMap<>();
                videoMap.put("id", eduVideo.getId());
                videoMap.put("filePath", eduVideo.getFilePath());
                videoMap.put("fileSize", eduVideo.getFileSize());
                videoMap.put("fileIdentifier", eduVideo.getFileIdentifier());
                videoMap.put("fileMins", eduVideo.getFileMins());
                videoMap.put("name", eduVideo.getName());
                videoMap.put("teacherId", eduVideo.getTeacherId());
                videoMap.put("teacherName", eduVideo.getName());
                videoMap.put("sort", eduVideo.getSort());
                videoMap.put("status", eduVideo.getStatus());
                videoMap.put("createdAt", eduVideo.getCreatedAt());
                videoMap.put("updatedAt", eduVideo.getUpdatedAt());
                videoMap.put("level", 2);
                videoArr.add(videoMap);
            }
            shutnumMap.put("videoList", videoArr);
//            eduShutnum.setVideoList(list);
            shutnumArr.add(shutnumMap);
        }

        //是否有错题 0-否，1-是
        Integer isChaTestError = 0;

        Map<String,Object> map = new HashMap<>();
        map.put("currentPageNum",iPage.getCurrent());
        map.put("courseId",eduCourse.getId());
        map.put("isChaTestError",isChaTestError);//是否有错题 0-否，1-是


        return new Result(Code.OK,new PageResult(shutnumArr,iPage.getTotal(),iPage.getPages(),map));
    }
}
